package com.comcast.core.eeg.eventsubscription.ejb;

import java.rmi.RemoteException;


import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.ejbgen.Constants;
import weblogic.ejbgen.JarSettings;
import weblogic.ejbgen.JndiName;
import weblogic.ejbgen.MethodPermissionPattern;
import weblogic.ejbgen.RemoteMethod;
import weblogic.ejbgen.RoleMapping;
import weblogic.ejbgen.Session;

import com.comcast.core.eeg.eventsubscription.service.EventSubscriptionServiceImpl;
import com.comcast.core.eeg.eventsubscription.types.DeliveryOption;
import com.comcast.core.eeg.eventsubscription.types.EventSubscription;
import com.comcast.core.eeg.eventsubscription.types.RetrievalResult;
import com.comcast.core.eeg.eventsubscription.types.RetrievingResult;
import com.comcast.core.eeg.eventsubscription.types.SubscriptionOption;
import com.comcast.core.eeg.eventsubscription.types.SubscriptionResult;
import com.comcast.core.eeg.types.ErrorMessage;
import com.comcast.core.eeg.types.Participant;
import com.comcast.core.eeg.types.Result;
import com.comcast.core.eeg.types.TransactionOption;
//import com.comcast.core.framework.util.ObjectFactory;

@Session(idleTimeoutSeconds = "500",
         maxBeansInCache = "40",
         allowConcurrentCalls = Constants.Bool.TRUE,
         cacheType = Session.CacheType.L_R_U,
         type = Session.SessionType.STATEFUL,
         transactionType = Session.SessionTransactionType.BEAN,
         ejbName = "EventSubscriptionServiceBean",
         remoteClientTimeout = "900",
         enableCallByReference = Constants.Bool.TRUE)

@JndiName(remote="com.comcast.core.eeg.eventsubscription.EventSubscriptionServiceBean")

@MethodPermissionPattern(pattern= "*", roles= "EEP_SUBSCRIBER")

@RoleMapping(roleName = "EEP_SUBSCRIBER", principals = "EepSubscribers")

@JarSettings(ejbClientJar = "EventSubscriptionService-client.jar")

public class EventSubscriptionServiceBean implements SessionBean {

	private static final long serialVersionUID = 1L;

    private static Log logger = LogFactory.getLog(EventSubscriptionServiceBean.class);

    private static final String UNKNOWN_ACTOR = "UNKNOWN";
    private static final String ONE_SPACE = "-";

    private static final String ROLLBACK_ERROR_CODE = "EVENTSUBSCRIPTION-1026 : ";

    //Passivated attributes
    private Participant participant;
    private String subscriptionName;
    private SubscriptionOption option;
    private EventSubscription sessionEventSubscription;

    //private SessionContext ctx = null;

    //transient attributes
    private transient EventSubscription eventSubscription;

    private transient EventSubscriptionServiceImpl impl;


	public EventSubscriptionServiceBean() {
		super();
        logger.info("EventSubscriptionServiceBean has been created.");
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext context)
        throws EJBException, RemoteException {
		//ctx = context;
	}

    public void ejbCreate() throws CreateException {
        logger.info("Creating stateful EventSubscriptionServiceBean ...");

        //impl = (EventSubscriptionServiceImpl)ObjectFactory.getBean("eeg.eventsubscription.EventSubscriptionService");
    }

	public void ejbRemove() throws EJBException, RemoteException {
        logger.info("Removing EventSubscriptionServiceBean for " + subscriptionName);
        cleanup(); //for scenario: client can NOT call close

        //final clean up - Passivated attributes
        this.participant = null;
        this.subscriptionName = null;
        this.option = null;
        this.sessionEventSubscription = null;
	}

	public void ejbActivate() throws EJBException, RemoteException {
        logger.info("Activating EventSubscriptionServiceBean for " + subscriptionName);

        try {
            ejbCreate();
        } catch (CreateException e) {
            throw new EJBException(e);
        }
        //re-subscribe
        SubscriptionResult result = subscribe(participant, subscriptionName, option);
        if (!result.getSucceeded()) {
            logger.warn("Failed to activate subscription service for " + subscriptionName);
            ErrorMessage[] msgs = result.getErrors();
            for (int i = 0; msgs != null && i < msgs.length; i++ ) {
                logger.warn("Error code: " + msgs[i].getCode());
            }

            throw new EJBException("Failed to activate subscription service.");
        }
	}

	public void ejbPassivate() throws EJBException, RemoteException {
        logger.info("Passivating EventSubscriptionServiceBean for " + subscriptionName);
        cleanup();
	}

    @RemoteMethod()
    public SubscriptionResult subscribe(Participant actor, String subName, SubscriptionOption subOption) {
    	return null;
    }

    @RemoteMethod()
    public RetrievalResult get(EventSubscription subscription, DeliveryOption deliveryOption) {
    	return null;
    }

    @RemoteMethod()
    public RetrievingResult retrieve(EventSubscription subscription, DeliveryOption deliveryOption) {
    	return null;
    }

    @RemoteMethod()
    public Result commit(EventSubscription subscription) {
    	return null;
    }

    @RemoteMethod()
    public Result rollback(EventSubscription subscription) {
    	return null;
    }

    @RemoteMethod()
    public void close(EventSubscription subscription) {
    }

    private void cleanup() {}


    private void reduceConnectionCount(Participant actor, String subName) {
    }


}
